package org.zw.android.framework.http;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.zw.android.framework.util.StringUtils;

/**
 * Http Request
 * 
 * @author zhouwei
 *
 */
public class HttpRequest {
	
	private String 		mUrl ;
	private int 		mConnectTimeout		= -1 ;
	private int 		mReadTimeout		= -1 ;
	private boolean 	mHasFilter ;
	
	private final Map<String, String> headerParamter 	= new HashMap<String, String>();
	private final Map<String, String> mParamter 		= new HashMap<String, String>();
	private final Map<String, File> mFileParamter 		= new HashMap<String, File>();
	
	public HttpRequest(String url){
		mUrl	= url ;
		doFilter(true);
	}
	
	public final void setConnectTimeout(int timeout){
		mConnectTimeout	= timeout ;
	}
	
	public final void setReadTimeout(int timeout){
		mReadTimeout	= timeout ;
	}
	
	public final int getConnectTimeout(){
		return mConnectTimeout ;
	}
	
	public final int getReadTimeout(){
		return mReadTimeout ;
	}
	
	public final void setUrl(String url){
		mUrl	= url ;
	}
	
	public final String getUrl(){
		return mUrl ;
	}

	public void doFilter(boolean hasFilter){
		mHasFilter	= hasFilter ;
	}
	
	public boolean isFilter(){
		return mHasFilter ;
	}
	
	public void addHeaderParameter(String key, String value){
		
		if(!StringUtils.isEmpty(key) && value != null){
			headerParamter.put(key, value);
		}
	}
	
	public void addParameter(String key, String value){
		
		if(!StringUtils.isEmpty(key) && value != null){
			mParamter.put(key, value);
		}
	}
	
	public void addFile(String key, File file){
		
		if(!StringUtils.isEmpty(key) && file != null){
			mFileParamter.put(key, file);
		}
	}
	
	public Map<String, String> getHeaderParameter() {
		return headerParamter;
	}

	public Map<String, String> getParameter() {
		return mParamter;
	}

	public Map<String, File> getFileParameter() {
		return mFileParamter;
	}

	@Override
	public String toString() {
		
		StringBuilder str = new StringBuilder() ;
		
		str.append("url=") ;
		str.append(mUrl);
		str.append("\n");
		
		// header
		Set<String> keys = headerParamter.keySet() ;
		for(String key : keys){
			str.append(key) ;
			str.append("=") ;
			str.append(headerParamter.get(key));
			str.append("\n");
		}
		
		// paramter
		keys = mParamter.keySet() ;
		for(String key : keys){
			str.append(key) ;
			str.append("=") ;
			str.append(mParamter.get(key));
			str.append("\n");
		}
		
		// File
		keys = mFileParamter.keySet() ;
		for(String key : keys){
			str.append(key) ;
			str.append("=") ;
			str.append(mFileParamter.get(key).getAbsolutePath());
			str.append("\n");
		}
		
		return str.toString();
	}
}
